package interview;

public class Leetcode_面试题_0106 {
    public String compressString(String S) {
        if (S.length() < 3) {
            return S;
        }
        StringBuilder sb = new StringBuilder(S);
        StringBuilder r = new StringBuilder();
        char t = sb.charAt(0);
        int n = 1;
        for (int i = 1; i < sb.length(); i++) {
            char c = sb.charAt(i);
            if (c == t) {
                n++;
            } else {
                r.append(t).append(n);
                t = c;
                n = 1;
            }
        }
        r.append(t).append(n);
        if (r.length() > sb.length()) {
            return S;
        } else {
            return r.toString();
        }
    }

    public static void main(String[] args) {

    }
}
